package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Amenity;

public class AmenityLogic extends AbstractBL {
	public List<Amenity> GenerateAmenity(long hotelId)
	{
		List<Amenity> list = new ArrayList<Amenity>();
		
		Amenity amenity = new Amenity();
		amenity.setHotelId(hotelId);
		amenity.setTitle("TV");
		amenity.setSortOrder(0);
		list.add(amenity);
		
		amenity = new Amenity();
		amenity.setHotelId(hotelId);
		amenity.setTitle("Bed");
		amenity.setSortOrder(0);
		list.add(amenity);
		
		amenity = new Amenity();
		amenity.setHotelId(hotelId);
		amenity.setTitle("Table");
		amenity.setSortOrder(0);
		list.add(amenity);
		
		return list;
	}
	
	public Amenity GetAmenity(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Amenity, id);
		try {
			Entity lang = datastore.get(key);
			Amenity c = new Amenity();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
		
	
	public Amenity GetAmenity(long hotelId,String title)
	{
		Query q = new Query(EntityName.Amenity);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetAmenity(datastore.prepare(q).asSingleEntity());
	}
	
	public List<Amenity> GetAllAmenity(long hotelId)
	{
		Query q = new Query(EntityName.Amenity);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filter = CompositeFilterOperator.and(filterHotel,filterStatus, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<Amenity> GetAllAmenity4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.Amenity);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}	
	
	public List<Amenity> GetAllAmenity4Admin(long hotelId)
	{
		Query q = new Query(EntityName.Amenity);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public Amenity GetAmenity(Entity entity)
	{
		Amenity item = new Amenity();
		item.setEntity(entity);
		return item;
	}
	
	public List<Amenity> GetList(List<Entity> entities)
	{
		List<Amenity> list = new ArrayList<Amenity>();
		for(Entity e : entities)
		{
			Amenity a = new Amenity();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public Amenity AddAmenity(long id,String description,String pic,String amenities) throws EntityNotFoundException{
		Amenity a = new Amenity();
		
		a.setHotelId(id);
		a.setTitle(amenities);
		a.setDescription(description);
		a.setPic(pic);
		a.setSortOrder(0);
		a.setAvaliable(true);
		a.setStatus(true);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
		
	}
	
	public Amenity EditAmenity(long amenId,String description,String pic,String amenities) throws EntityNotFoundException{
		Amenity a = GetAmenity(amenId);
		a.setTitle(amenities);
		a.setDescription(description);
		if(pic != null){
			a.setPic(pic);
		}
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}

	public Amenity DeleteAmenity(long amenId) throws EntityNotFoundException{
		
		Amenity a = GetAmenity(amenId);
		a.setAvaliable(false);
		a.setStatus(false);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public Amenity ChangeStatus(long amenId) throws EntityNotFoundException{
		
		Amenity a = GetAmenity(amenId);
		if(a.getStatus() == true){
			a.setStatus(false);
		}else if(a.getStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public Amenity EditSortOrder(long amenId,long sortOrder) throws EntityNotFoundException{
		
		Amenity a = GetAmenity(amenId);
		a.setSortOrder(sortOrder);
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
}
